package com.beiing.tianshuai.tianshuai.mine.model;

import com.beiing.tianshuai.tianshuai.mine.entity.OrderApplyAfterSaleBean;
import com.beiing.tianshuai.tianshuai.mine.entity.OrderApplyAfterSalePostDataBean;
import com.beiing.tianshuai.tianshuai.network.DataManager;
import com.beiing.tianshuai.tianshuai.util.LogUtils;

import java.io.File;
import java.util.List;

import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.observers.DisposableObserver;
import io.reactivex.schedulers.Schedulers;
import okhttp3.MediaType;
import okhttp3.MultipartBody;
import okhttp3.RequestBody;

/**
 *申请售后返回订单信息model
 */
public class OrderApplyAfterSaleModel implements OrderApplyAfterSaleModelImpl{

    private OrderApplyAfterSaleModel.OrderApplyAfterSaleDataResponseListener mListener;
    private OrderApplyAfterSaleModel.OrderApplyAfterSaleDataPostResponseListener mPostListener;


    private static final String TAG = "OrderApplyAfterSaleModel";

    public OrderApplyAfterSaleModel(OrderApplyAfterSaleModel.OrderApplyAfterSaleDataResponseListener listener,
                                    OrderApplyAfterSaleModel.OrderApplyAfterSaleDataPostResponseListener postListener) {
        mListener = listener;
        mPostListener=postListener;
    }
    public interface OrderApplyAfterSaleDataResponseListener{
        void onGetDetailSuccess(OrderApplyAfterSaleBean bean);
        void onGetDetailError(Throwable throwable);
    }
    public interface OrderApplyAfterSaleDataPostResponseListener{
        void onPostDetailSuccess(OrderApplyAfterSalePostDataBean bean);
        void onPostDetailError(Throwable throwable);
    }

    @Override
    public void getOrderApplyAfterSale(String mOrdersId) {
        RequestBody body = new MultipartBody.Builder().setType(MultipartBody.FORM)
                .addFormDataPart("orders_id", mOrdersId)
                .build();
        io.reactivex.Observable<OrderApplyAfterSaleBean> observable = DataManager.getOrderApplyAfterSaleData(body);
        io.reactivex.Observable<OrderApplyAfterSaleBean> observeOn = observable.subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread());
        observeOn.subscribe(new DisposableObserver<OrderApplyAfterSaleBean>() {
            @Override
            public void onNext(OrderApplyAfterSaleBean bean) {
                if (mListener != null) {
                    mListener.onGetDetailSuccess(bean);
                }
            }

            @Override
            public void onError(Throwable e) {
                if (mListener != null) {
                    LogUtils.e(e.getMessage(), TAG);
                    mListener.onGetDetailError(e);
                }
            }
            @Override
            public void onComplete() {

            }
        });
    }

    /**
     *   获取申请售后上传货物信息返回值
     * @param mOrdersId 订单id
     * @param mGoodsState 货物状态
     * @param mReturnReason 退货原因
     * @param mReturnExplain 退货说明
     * @param mImagesPath 退货凭证图片
     */
    @Override
    public void postOrderApplyAfterSale(String mOrdersId,String mGoodsState,String mReturnReason,
                                        String mReturnExplain,String mReturnType,List<String> mImagesPath) {
        MultipartBody.Builder builder = new MultipartBody.Builder().setType(MultipartBody.FORM);
        builder.addFormDataPart("orders_id", mOrdersId)
                .addFormDataPart("logistics", mGoodsState)
                .addFormDataPart("reason", mReturnReason)
                .addFormDataPart("explain", mReturnExplain)
                .addFormDataPart("refund_type",mReturnType)
                .build();
        for (String imgPath : mImagesPath) {
            File file = new File(imgPath);
            builder.addFormDataPart(
                    "voucher[]",
                    file.getName(),
                    RequestBody.create(MediaType.parse("images/*"), file)
            );
        }
        RequestBody requestBody = builder.build();

        io.reactivex.Observable<OrderApplyAfterSalePostDataBean> observable = DataManager.postOrderApplyAfterSaleData(requestBody);
        io.reactivex.Observable<OrderApplyAfterSalePostDataBean> observeOn = observable.subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread());
        observeOn.subscribe(new DisposableObserver<OrderApplyAfterSalePostDataBean>() {
            @Override
            public void onNext(OrderApplyAfterSalePostDataBean bean) {
                if (mPostListener != null) {
                    mPostListener.onPostDetailSuccess(bean);
                }
            }

            @Override
            public void onError(Throwable e) {
                if (mPostListener != null) {
                    LogUtils.e(e.getMessage(), TAG);
                    mPostListener.onPostDetailError(e);
                }
            }
            @Override
            public void onComplete() {

            }
        });
    }
}

